package lch.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lch.dao.MemberDao;
import lch.dao.OrderListDao;
import lch.dao.SetmealDao;
import lch.entity.PageResult;
import lch.entity.QueryPageBean;
import lch.pojo.CheckItem;
import lch.pojo.Member;
import lch.service.OrderListService;
import lch.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

import java.util.Map;

/**
 * @author lch
 * @date 2020-01-16 16:48
 */
@Service(interfaceClass = OrderListService.class)
@Transactional
public class OrderListServiceImpl implements OrderListService {
    @Autowired
    private OrderListDao orderListDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private SetmealDao setmealDao;

    @Override
    public PageResult findList(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Map> page = orderListDao.findList(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void addList(Map map) {
        //先创会员
        Member member = new Member();
        member.setName((String) map.get("memberName"));
        member.setPhoneNumber((String) map.get("phoneNumber"));
        member.setRegTime(new Date());
        memberDao.add(member);
        //拿到会员id
        Integer member_id = member.getId();
        //拿到套餐id
        String setmeal_name = (String) map.get("setmealName");
        Integer setmeal_id = setmealDao.findIdByName(setmeal_name);
        //新增
        map.put("member_id", member_id);
        map.put("setmeal_id", setmeal_id);

        orderListDao.addList(map);
    }


    @Override
    public Map findById(Integer id) {
        Map map = orderListDao.findById(id);
        //        if (map == null) {
        //            new RuntimeException();
        //        }
        return map;
    }

    @Override
    public void editList(Map map) {
        //拿到套餐id
        String setmeal_name = (String) map.get("setmealName");
        Integer setmeal_id = setmealDao.findIdByName(setmeal_name);
        //修改map内容
        map.put("setmeal_id", setmeal_id);
        //修改t_order表
        orderListDao.editList(map);

    }

    @Override
    public void deleteById(Integer id) {
        orderListDao.deleteById(id);
    }
}